using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace IQIGame.Onigao.Game.Editors
{
    [CustomPropertyDrawer(typeof(EntityGroupInfo))]
    public class EntityGroupInfoPropertyDrawer : PropertyDrawer
    {
        private VisualTreeAsset m_InspectorXML;
        private float typeOptionLastUpdateTime;
        private List<string> m_TypeOption;
        private Enum m_Group;

        private List<string> TypeOptions
        {
            get
            {
                if (Time.realtimeSinceStartup - typeOptionLastUpdateTime > 5)
                {
                    typeOptionLastUpdateTime = Time.realtimeSinceStartup;
                    if (m_TypeOption == null)
                    {
                        m_TypeOption = new List<string>();
                    }
                    m_TypeOption.Clear();
                    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                    Type entityType = typeof(Entity);
                    for (int i = 0; i < assemblies.Length; i++)
                    {
                        Type[] types = assemblies[i].GetTypes();
                        foreach (Type type in types)
                        {
                            if (!type.IsAbstract && entityType.IsAssignableFrom(type))
                            {
                                m_TypeOption.Add(type.FullName);
                            }
                        }
                    }
                }
                return m_TypeOption;
            }
        }

        private Enum Group
        {
            get
            {
                if (m_Group == null)
                {
                    m_Group = GetEnum("IQIGame.Onigao.GamePlay.EntityPoolGroupType");
                }
                return m_Group;
            }
        }

        public static Enum GetEnum(string enumFullName)
        {
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                Type type = assembly.GetType(enumFullName);
                if (type != null && type.IsEnum)
                {
                    return (Enum)type.GetEnumValues().GetValue(0);
                }
            }

            return null;
        }

        public override VisualElement CreatePropertyGUI(SerializedProperty property)
        {
            // Create a new VisualElement to be the root of our inspector UI
            VisualElement myInspector = new VisualElement();

            if (m_InspectorXML == null)
            {
                m_InspectorXML = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Scripts/FrameworkExt/Editor/Entity/EntityGroupInfo.uxml");
            }
            m_InspectorXML.CloneTree(myInspector);

            Label label = myInspector.Q<Label>("Label");
            SerializedProperty enumProperty = property.FindPropertyRelative("m_Id");
            Enum defaultEnumValue = enumProperty == null ? null : (Enum)Enum.ToObject(Group.GetType(), enumProperty.intValue);

            label.text = defaultEnumValue.ToString();

            // 创建一个新的PopupField并将选项列表传递进去
            EnumField popupId = new EnumField("Group", defaultEnumValue);
            myInspector.Insert(1, popupId);
            popupId.RegisterValueChangedCallback(evt =>
            {
                enumProperty.intValue = Convert.ToInt32(evt.newValue);
                enumProperty.serializedObject.ApplyModifiedProperties();

                label.text = evt.newValue.ToString();
            });

            // 创建一个新的PopupField并将选项列表传递进去
            PopupField<string> popupType = new PopupField<string>("EntityType", TypeOptions, 0);
            popupType.bindingPath = "m_EntityTypeName";
            myInspector.Add(popupType);

            // Return the finished inspector UI
            return myInspector;
        }
    }
}
